package fr.orleans.converter;

import java.util.List;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import entities.Tournament;
import fr.orleans.tools.EJBServices;
import fr.orleans.tools.JSFServices;

@FacesConverter("tournamentConverter")
public class TournamentConverter implements Converter{

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		if(arg2 != null && arg2 != ""){
			EJBServices services = (EJBServices) JSFServices.getManagedBean(EJBServices.class, "eJBServices");
			List<Tournament> listTournament = services.getWebAction().getListTournamentByFlagActivated();
			
			for (Tournament tour : listTournament) {
				if (tour.getName().equals(arg2))
					return tour;		
			}
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) {
		if(arg2 != null){
			if(arg2 instanceof Tournament){
				Tournament tour = (Tournament) arg2;
				return tour.getName();
			}
		}
		return null;
	}
}